$(function(){
	searchAuthByDept();
	/*选择框移动*/
	$("#add").click(function(){
		/*if($("#select1 option:selected").size()==0){
			show_err_msg('请选择权限！');
		}else{*/
			if(1)
				distributRight();
			else
				show_err_msg("您目前没有给角色分配权限的权限,请联系管理员");
		//}
	});
		
	$("#remove").click(function(){
		if($("#select2 option:selected").size()==0){
			show_err_msg('请选择权限！');
		}else{
			if(ajaxFun("/ajax/checkAuthority/?functionId=web_role_setPermit&deptId="))
				distributLeftCheck();
			else
				show_err_msg("您目前没有给角色移除权限的权限,请联系管理员");
		}	
	});	
		
	$("#remove_all").click(function(){
		if($("#select2 option").size()==0){
			show_err_msg('该项没有可操作的权限！');
		}else{
			if(ajaxFun("/ajax/checkAuthority/?functionId=web_role_setPermit&deptId="))
				distributLeftAllCheck();
			else
				show_err_msg("您目前没有给用户移除权限的权限,请联系管理员");
		}		
	});	
});
function distributRight(){
	var str = new String;
	for(var i=0;i<$(':input[name="check"]').length;i++){
		if($(':input[name="check"]').eq(i).attr('checked')){
			str += '<input type="checkbox" value="'+$(':input[name="check"]').eq(i).val()+'" name="check1">'+$(':input[name="check"]').eq(i).val();
			$(':input[name="check"]').eq(i).parent().remove();
			//alert(i);
		}
	}
	$('#tarTree').append(str);
	/*var dept = $("#dept").val();
	var functionId = new Array();
	var roleId = $("#roleId").val();
	var subSystemList = new Array();
	$("#select1 option:selected").each(function(){
		var byDeptValue = $(this).attr("byDept");
		functionId.push($(this).val());
		subSystemList.push($(this).attr("subSystem"));
	});
	if(functionId.length == 0){
		show_err_msg("请选择权限");
		return false;
	}else{
		var xmlhttp;
	    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
	        xmlhttp = new XMLHttpRequest();
	    }else {// code for IE6, IE5
	        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    xmlhttp.onreadystatechange = function () {
	        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
	            var obj;
	            try{
	                obj =  eval('(' + xmlhttp.responseText + ')');
	            }catch(exception){
	                obj="error";
	            }
	            if(obj == "error"){
	                show_err_msg("服务链接出错！");    
	            }else{
	                if(obj.status == "succeed"){
						show_msg(obj.info,'');
						searchAuthByDept();
	                }else if(obj.status == "fail"){
						show_err_msg(obj.info);
	                }
	            }
	        }
	    }
	    xmlhttp.open("POST", "/ajax/distributRight/", true);
	    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	    xmlhttp.send("dept="+dept+"&functionId="+functionId+"&roleId="+roleId+"&subSystemList="+subSystemList)
	}*/
	
}

function distributLeftCheck(){
	var functionId = new Array();
	var roleId = $("#roleId").val();
	var dept = $("#dept").val();
	$("#select2 option:selected").each(function(){
		functionId.push($(this).val());
	});
	if(dept==''){
		show_err_msg("请选择部门");
		return false;
	}
	distributLeft(functionId,roleId,dept);
}
function distributLeft(functionId,roleId,dept){
	var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj;
            try{
                obj =  eval('(' + xmlhttp.responseText + ')');
            }catch(exception){
                obj="error";
            }
            if(obj == "error"){
                show_err_msg("服务链接出错！");    
            }else{
                if(obj.status == "succeed"){
                	show_msg(obj.info,'');
            		searchAuthByDept();
                }else if(obj.status == "fail"){
					show_err_msg(obj.info);
                }
            }
        }
    }
    xmlhttp.open("POST", "/ajax/distributLeft/", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send("functionId="+functionId+"&roleId="+roleId+"&dept="+dept)
}
function distributLeftAllCheck(){
	var functionId = new Array();
	var roleId = $("#roleId").val();
	var dept = $("#dept").val();
	$("#select2 option").each(function(){
		functionId.push($(this).val());
	});
	if(dept==''){
		show_err_msg("请选择部门");
		return false;
	}
	distributLeft(functionId,roleId,dept);
}
function searchAuthByDept(){
	var dept = $("#dept").val();
	var roleId =$("#roleId").val();
	var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var obj;
            try{
                obj =  eval('(' + xmlhttp.responseText + ')');
            }catch(exception){
                obj="error";
            }
            if(obj == "error"){
                show_err_msg("服务链接出错！");    
            }else{
                if(obj.status == "succeed"){
					var outOption = $("#select1");
				    var ownOption = $("#select2");
				    outOption.html("");
				    ownOption.html("");
				    outOption.html(obj.outFunction);
				    ownOption.html(obj.ownFunction);
				}else if(obj.status == "fail"){
					show_err_msg(obj.info);
	            }
        	}
    	}
    }
    xmlhttp.open("POST", "/ajax/searchAuthByDept/", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send("deptId="+dept+"&roleId="+roleId)
}